﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Utils;

///Author: NHDuc
///Created date: 22/11/2013
namespace VietThuongLMS.Services
{
    public class HinhAnhService
    {
        public List<HinhAnh> GetHinhAnhList()
        {
            try
            {
                using (LMSDataEntities context = new LMSDataEntities())
                {
                    List<HinhAnh> list = context.HinhAnhs.ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("HinhAnhService::GetHinhAnhList", ex.StackTrace, ex.Message);
                return null;
            }
        }

        public static HinhAnhViewModel GetHinhAnhList(int pageNum, int pageIndex)
        {
            //LMSDataEntities context = new LMSDataEntities();
            //try
            //{
            //    //hinh anh
            //    List<HinhAnh> list = context.HinhAnhs.Where(m => m.Type == 1).OrderByDescending(m => m.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
            //    int total = context.HinhAnhs.Count();
            //    //video
            //    List<HinhAnh> videoList = context.HinhAnhs.Where(m => m.Type == 2).OrderByDescending(m => m.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
            //    int videoTotal = context.HinhAnhs.Count();

            //    return new HinhAnhViewModel(list, total, videoList, videoTotal);
            //}
            //catch (Exception ex)
            //{
            //    DCS.ComLib.File.Log.Write("AccountService::GetHinhAnhList", ex.StackTrace, ex.Message);
                return new HinhAnhViewModel();
            //}
            //finally
            //{
            //    context.Dispose();
            //}
        }

        public static HinhAnhViewModel GetHinhAnhAllList(int pageNum, int pageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                List<HinhAnh> list = context.HinhAnhs.OrderByDescending(m => m.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.HinhAnhs.Count();

                return new HinhAnhViewModel(list, total);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetHinhAnhAllList", ex.StackTrace, ex.Message);
                return new HinhAnhViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static HinhAnhViewModel GetHinhAnhAllList(int id, int pageNum, int pageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                List<HinhAnh> list = context.HinhAnhs.Where(w => w.AlbumID == id).OrderByDescending(m => m.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.HinhAnhs.Where(w => w.AlbumID == id).Count();

                return new HinhAnhViewModel(list, total);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetHinhAnhAllList", ex.StackTrace, ex.Message);
                return new HinhAnhViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static HinhAnhViewModel GetHinhAnhSepList(int pageNum, int pageIndex, int VpageNum, int VpageIndex)
        {
            //LMSDataEntities context = new LMSDataEntities();
            //try
            //{
            //    //hinh anh
            //    List<HinhAnh> list = context.HinhAnhs.Where(m => m.Type == 1).OrderByDescending(m => m.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
            //    int total = context.HinhAnhs.Count();
            //    //video
            //    List<HinhAnh> videoList = context.HinhAnhs.Where(m => m.Type == 2).OrderByDescending(m => m.ID).Skip(VpageNum * (VpageIndex - 1)).Take(VpageNum).ToList();
            //    int videoTotal = context.HinhAnhs.Count();

            //    return new HinhAnhViewModel(list, total, videoList, videoTotal);
            //}
            //catch (Exception ex)
            //{
            //    DCS.ComLib.File.Log.Write("AccountService::GetHinhAnhList", ex.StackTrace, ex.Message);
                return new HinhAnhViewModel();
            //}
            //finally
            //{
            //    context.Dispose();
            //}
        }

        public static HinhAnhViewModel GetHinhAnhByAlbumID(int id, int type, int pageNum, int pageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                //hinh anh
                List<HinhAnh> list = context.HinhAnhs.Where(m => m.AlbumID == id).OrderByDescending(m => m.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.HinhAnhs.Count();

                return new HinhAnhViewModel(list, total, type);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetHinhAnhByAlbumID", ex.StackTrace, ex.Message);
                return new HinhAnhViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool InsertHinhAnh(HinhAnh comp)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                context.AddToHinhAnhs(comp);
                context.SaveChanges();
                return true;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("HinhAnhServices::InsertHinhAnh", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool Edit(HinhAnh newModel)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                HinhAnh model = context.HinhAnhs.Where(c => c.ID == newModel.ID).FirstOrDefault();
                if (model != null)
                {
                    model.TenAnh = newModel.TenAnh;
                    model.Url = newModel.Url;
                    //model.Tag = newModel.Tag;
                    //model.Type = newModel.Type;
                    context.SaveChanges();
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("HinhAnhServices::Edit", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static HinhAnh GetHinhAnhById(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                return context.HinhAnhs.Where(c => c.ID == id).FirstOrDefault();

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("HinhAnhServices::GetHinhAnhById", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool DeleteHinhAnh(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                HinhAnh obj = context.HinhAnhs.Where(c => c.ID == id).FirstOrDefault();
                if (obj != null)
                {
                    context.DeleteObject(obj);
                    context.SaveChanges();
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("HinhAnhServices::DeleteHinhAnh", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }



    }
}